package com.atguigu05.objectstream;

import org.junit.Test;

import java.io.*;

/**
 * ClassName: ObjectStreamTest
 * Package: com.atguigu05.objectstream
 * Description:
 *
 * @Author BuTang
 * @Create 2023/6/15 18:31
 * @Version 1.0
 */
public class ObjectStreamTest {
    @Test
    public void test1(){
        File oldFile = new File("hello.txt");
        Person p = new Person("Tom",12);
        //需要序列化
        ObjectOutputStream obj = null;

        try {
            obj = new ObjectOutputStream(new FileOutputStream(oldFile) );
            obj.writeUTF("江山如此多焦");
            obj.writeChar('\t');
            obj.writeObject(p);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (obj != null)
                obj.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }

    @Test
    public void test2(){
        File newFile = new File("hello.txt");

        //反序列化
        String s = null;

        try {
            ObjectInputStream obj = new ObjectInputStream(new FileInputStream(newFile));


            s = obj.readUTF();
//            Person p = (Person) obj.readObject();
//            p.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        } finally {

        }


        System.out.println(s);

    }
    }


